package com.bluetoothsms.common;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class MessageConversationsList extends BluetoothMessage {

	public MessageConversationsList(Conversation[] list) {
		super(BtMessageType.ThreadList, null);

		mData = new String[list.length * Conversation.FIELD_COUNT];

		int i = 0;
		for (Conversation cv : list) {
			mData[i++] = cv.getPhoneNumber();
			mData[i++] = cv.getContactName();
			mData[i++] = String.valueOf(cv.getThreadId());
			mData[i++] = String.valueOf(cv.getMessageCount());
			mData[i++] = cv.getSnippet();
		}
	}

	public MessageConversationsList(byte[] data, int length) {
		super(BtMessageType.ThreadList, data, length);
	}

	public Collection<Conversation> getThreadList() {
		List<Conversation> list = null;

		if (mData.length % Conversation.FIELD_COUNT == 0) {
			list = new ArrayList<Conversation>(mData.length
					/ Conversation.FIELD_COUNT);

			for (int i = 0; i < mData.length; i += Conversation.FIELD_COUNT) {
				list.add(new Conversation(mData, i));
			}
		}

		return list;
	}

}
